package com.huang;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

public class TestAuthenticator {
    public static void main(String[] args) {
        //1 创建安全管理器对象
        DefaultSecurityManager securityManager=new DefaultSecurityManager();
        //给安全管理器设置realm
        securityManager.setRealm(new IniRealm("classpath:shiro.ini"));
        //3 SecurityUtills 给全局安全工具类设置安全管理器
        SecurityUtils.setSecurityManager(securityManager);
        //4 关键对象 subject登录的主体
        Subject subject = SecurityUtils.getSubject();
        //5创建令牌
        UsernamePasswordToken token = new UsernamePasswordToken("xiaohuang","123");
        try {
            System.out.println(subject.isAuthenticated());
            subject.login(token);//用户认证
            System.out.println(subject.isAuthenticated());

        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
    }
}
